package com.ms.oschina.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;

import com.ms.oschina.api.remote.OsChinaApi;
import com.ms.oschina.base.BaseViewHolder;
import com.ms.oschina.bean.Constants;
import com.ms.oschina.bean.Tweet;
import com.ms.oschina.tagpager.TweetFragment;
import com.ms.oschina.ui.activity.TweetsDetailsActivity;
import com.ms.oschina.util.PreferenceUtils;
import com.ms.oschina.util.UiUtils;
import com.ms.oschina.viewholder.TweetViewHolder;

public class TweetsAdapter extends PageIndexListAdapter<Tweet> {

	public TweetsAdapter(AbsListView listView, List<Tweet> list,
			boolean loadMore) {
		super(listView, list, loadMore);
	}

	@Override
	public void onNormalItemClick(AdapterView<?> parent, View view,
			int position, long id) {
		Tweet tweet = getListData().get(position);
		// 开启新闻详情界面
		Context context = UiUtils.getContext();
		Intent intent = new Intent(context, TweetsDetailsActivity.class);
		intent.putExtra(Constants.TWEETSDETAILSID, tweet.getId());
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	@Override
	public BaseViewHolder<Tweet> getItemViewHolder() {
		return new TweetViewHolder(getListData());
	}

	@Override
	public List<Tweet> getLoadMoreData() throws Exception {
		int mark = getMark();
		switch (mark) {
		case TweetFragment.NEWESTTWIST:
			return OsChinaApi.getNewesTwist(OsChinaApi.NEWESTTWIST, pageIndex)
					.getList();
		case TweetFragment.HOTSPOTTWIST:
			return OsChinaApi.getNewesTwist(OsChinaApi.HOTSPOTTWIST, pageIndex)
					.getList();
		case TweetFragment.MYTWIST:
			int uid = PreferenceUtils.getPrefInt(UiUtils.getContext(),
					Constants.UID, OsChinaApi.MYTTWIST);
			return OsChinaApi.getNewesTwist(uid, pageIndex).getList();
		}
		return null;
	}

}
